<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Upload extends Controller
{

    // 上传图片
    public function images(Request $request)
    {
        //获取图片
        $file = $request->file('file');

        //判断是否选择图片
        if ($file == null) {
            error('请上传文件');
        }

        //执行移动图片到指定目录
        $info = $file->validate(array('size'=>20971520,'ext'=>'jpg,jpeg,png,gif'))->move('./uploads/img/');

        if ($info) {
            //返回图片路径
            $path = '/uploads/img/' . str_replace('\\','/',$info->getSaveName());

            exit(json_encode(array('code'=>0, 'msg'=>'图片成功上传', 'data'=>array('src'=>$path, 'title'=>$path, 'url'=>$path))));
        } else {
            error($file->getError());
//            exit(json_encode(array('code'=>2, 'msg'=>$file->getError())));
        }

    }

    // 上传视频
    public function video(Request $request)
    {
        //获取图片
        $file = $request->file('file');

        //判断是否选择图片
        if ($file == null) {
            error('请上传文件');
        }

        //执行移动图片到指定目录
        $info = $file->validate(array('size'=>20971520))->move('./uploads/video/');

        if ($info) {
            //返回图片路径
            $path = '/uploads/video/' . str_replace('\\','/',$info->getSaveName());

            exit(json_encode(array('code'=>0, 'msg'=>'视频成功上传', 'data'=>array('src'=>$path, 'title'=>$path, 'url'=>$path))));
        } else {
            error($file->getError());
//            exit(json_encode(array('code'=>2, 'msg'=>$file->getError())));
        }

    }
    
}
